package org.lq.er.view.exam_manage;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.lq.er.entity.Exam_position;
import org.lq.er.service.Exam_positionService;
import org.lq.er.service.impl.Exam_positionServiceImpl;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * 修改考场信息 
 * @author 韩伟伟
 *
 * @package org.lq.er.view.exam_manage
 */
 
public class V_examPosition_update extends JFrame {

	private JPanel contentPane;
	private JTextField exp_position;
	private JTextField exp_conut;
	private static Exam_position exam_position = null;
	private Exam_positionService ps = new Exam_positionServiceImpl();
	private JTextField exp_no;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_examPosition_update frame = new V_examPosition_update(exam_position);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_examPosition_update(Exam_position exam_position) {
		setTitle("修改学生信息");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 314, 278);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.white);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		
		JLabel lbl_phoneNo = new JLabel("考场位置:");
		lbl_phoneNo.setBounds(54, 87, 54, 15);
		
		JLabel lbl_classes = new JLabel("考场人数:");
		lbl_classes.setBounds(54, 127, 54, 15);
		
		exp_position = new JTextField();
		exp_position.setBounds(118, 84, 136, 21);
		exp_position.setColumns(10);
		
		exp_conut = new JTextField();
		exp_conut.setBounds(118, 124, 136, 21);
		exp_conut.setColumns(10);
		panel.setLayout(null);
		
		panel.add(lbl_phoneNo);
		panel.add(lbl_classes);
		panel.add(exp_conut);
		panel.add(exp_position);
		
		JButton btn_save = new JButton("保存");
		btn_save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 创建考场对象
				Exam_position exp = new Exam_position();
				exp.setId(Integer.parseInt(exp_no.getText()));
				exp.setPosition(exp_position.getText());
				exp.setMax_count(Integer.parseInt(exp_conut.getText()));
				
				// 询问是否修改
				int num = JOptionPane.showConfirmDialog(V_examPosition_update.this, "是否修改?", "提示", JOptionPane.INFORMATION_MESSAGE);
				if (num == 0) {
					// 修改操作
					if (ps.update(exp)) {
						JOptionPane.showMessageDialog(V_examPosition_update.this, "修改成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
					} else {
						JOptionPane.showMessageDialog(V_examPosition_update.this, "修改失败!", "错误", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
		btn_save.setBounds(99, 176, 93, 23);
		panel.add(btn_save);
		
		exp_no = new JTextField();
		exp_no.setEditable(false);
		exp_no.setBounds(118, 43, 136, 21);
		panel.add(exp_no);
		exp_no.setColumns(10);
		
		JLabel lbl_phoneNo2 = new JLabel("编号:");
		lbl_phoneNo2.setBounds(78, 46, 30, 15);
		panel.add(lbl_phoneNo2);
		
		initText(exam_position);
	}
	
	/**
	 * 初始化文本内容
	 * @param exam_position
	 */
	private void initText(Exam_position exam_position) {
		exp_no.setText(castString(exam_position.getId()));
		exp_position.setText(exam_position.getPosition());
		exp_conut.setText(castString(exam_position.getMax_count()));
	}
	
	/**
	 * 	转成字符串
	 * @param obj
	 * @return
	 */
	public static String castString(Object obj) {
		return castString(obj, "");
	}

	/**
	 * 
	 * @param obj
	 * @param defaultValue
	 * @return
	 */
	public static String castString(Object obj,String defaultValue) {
		return obj != null ? String.valueOf(obj) : defaultValue;
	}
}
